package info.vanbeem.WikiMindDesktop;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class SettingsDialog extends Dialog {

	protected Object result;
	protected Shell shlWikimindDesktopSettings;
	private Text txtRootpath;
	private Text txtTemplatefile;
	private Settings settings;

	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public SettingsDialog(Shell parent, int style, Settings settings) {
		super(parent, style);
		this.settings = settings;
		setText("WikiMind Desktop settings");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shlWikimindDesktopSettings.open();
		shlWikimindDesktopSettings.layout();
		Display display = getParent().getDisplay();
		while (!shlWikimindDesktopSettings.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shlWikimindDesktopSettings = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);
		shlWikimindDesktopSettings.setSize(537, 202);
		shlWikimindDesktopSettings.setLayout(new FormLayout());
		
		Composite composite = new Composite(shlWikimindDesktopSettings, SWT.NONE);
		FormData fd_composite = new FormData();
		fd_composite.top = new FormAttachment(0, 10);
		fd_composite.left = new FormAttachment(0, 10);
		fd_composite.right = new FormAttachment(100, -10);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(3, false));
		
		Label lblDocumentroot = new Label(composite, SWT.NONE);
		lblDocumentroot.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblDocumentroot.setText("Document &root");
		
		txtRootpath = new Text(composite, SWT.BORDER);
		txtRootpath.setText(settings.getRootPath());
		txtRootpath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnSelectRoot = new Button(composite, SWT.NONE);
		btnSelectRoot.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog (shlWikimindDesktopSettings);
				dialog.setFilterPath(txtRootpath.getText());
				String result = dialog.open();
				if (result != null) {
					txtRootpath.setText(result);
				}
			}
		});
		btnSelectRoot.setText("...");
		
		Label lblHtmltemplate = new Label(composite, SWT.NONE);
		lblHtmltemplate.setText("HTML &template");
		
		txtTemplatefile = new Text(composite, SWT.BORDER);
		txtTemplatefile.setText(settings.getTemplateFile());
		txtTemplatefile.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		Button btnSelectTemplate = new Button(composite, SWT.NONE);
		btnSelectTemplate.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FileDialog dialog = new FileDialog (shlWikimindDesktopSettings, SWT.OPEN);
				String [] filterNames = new String [] {"html files", "all files (*)"};
				String [] filterExtensions = new String [] {"*.html", "*"};
				dialog.setFilterNames (filterNames);
				dialog.setFilterExtensions (filterExtensions);
				dialog.setFileName(txtTemplatefile.getText());
				String result = dialog.open();
				if (result != null) {
					txtTemplatefile.setText(result);
				}
			}
		});
		btnSelectTemplate.setText("...");
		new Label(composite, SWT.NONE);
		
		Button btnNewlineasparagraph = new Button(composite, SWT.CHECK);
		btnNewlineasparagraph.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnNewlineasparagraph.setText("Interpret &newline as paragraph");
		new Label(composite, SWT.NONE);
		
		Button btnAutosave = new Button(composite, SWT.CHECK);
		btnAutosave.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		btnAutosave.setText("Automatically &save the document when switching views");
		
		Composite composite_1 = new Composite(shlWikimindDesktopSettings, SWT.NONE);
		fd_composite.bottom = new FormAttachment(100, -53);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		composite_1.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite_1 = new FormData();
		fd_composite_1.top = new FormAttachment(100, -35);
		fd_composite_1.bottom = new FormAttachment(100, -10);
		fd_composite_1.left = new FormAttachment(0, 280);
		fd_composite_1.right = new FormAttachment(100, -10);
		composite_1.setLayoutData(fd_composite_1);
		
		Button btnOk = new Button(composite_1, SWT.NONE);
		btnOk.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				storeSettings();
				shlWikimindDesktopSettings.close();
			}
		});
		btnOk.setText("&OK");
		
		Button btnCancel = new Button(composite_1, SWT.NONE);
		btnCancel.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shlWikimindDesktopSettings.close();
			}
		});
		btnCancel.setText("&Cancel");
	}

	private void storeSettings() {
		settings.setRootPath(txtRootpath.getText());
		settings.setTemplateFile(txtTemplatefile.getText());
	}	
}
